package com.guoxue.util;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import com.guoxue.domain.BookConfigVo;
import com.guoxue.domain.LevelConfigVo;
import com.guoxue.domain.PlanChapterConfigVo;
import com.guoxue.domain.PlanConfigVo;
import com.guoxue.domain.RecordConfigVo;
import com.guoxue.domain.SwitchSettingVo;
import com.guoxue.service.IBookConfigService;
import com.guoxue.service.ILevelConfigService;
import com.guoxue.service.IPlanChapterConfigService;
import com.guoxue.service.IPlanConfigService;
import com.guoxue.service.IRecordConfigService;
import com.guoxue.service.ISwitchSettingService;

import redis.clients.jedis.Jedis;

@Component
public class LoadConfigProperties {
	@Resource
	private IPlanConfigService planConfig;
	@Resource
	private IBookConfigService bookConfig;
	@Resource
	private IRecordConfigService recordConfig;
	@Resource
	private IPlanChapterConfigService planChapterConfig;
	@Resource
	private ILevelConfigService levelConfigService;
	@Resource
	private ILevelConfigService levelConfig;
	@Resource
	private ISwitchSettingService switchSettingService;
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, String> template; // inject the template as ListOperations
    //至于这个为什么可以注入。需要参考AbstractBeanFactory doGetBean
    //super.setValue(((RedisOperations) value).opsForValue());就这一行代码  依靠一个editor
    @Resource(name = "redisTemplate")
    private ValueOperations<String, Object> vOps;
	public static Map<Integer, PlanConfigVo> planConfigMap = null;

	public static Map<Integer, BookConfigVo> bookConfigMap = null;

	public static Map<Integer, RecordConfigVo> recordConfigMap = null;

	public static Map<Integer, PlanChapterConfigVo> planChapterConfigMap = null;
	
	public static Map<Integer,LevelConfigVo> levelConfigMap = null;

	public static Map<String,SwitchSettingVo> switchSettingMap = null;
	
	public void loadValue() {
		Jedis jedis = RedisUtil.getJedis();
		try {
			planConfigMap = (Map<Integer, PlanConfigVo>) RedisUtil.unserialize(jedis.get("planConfigStatic".getBytes()));
			bookConfigMap = (Map<Integer, BookConfigVo>) RedisUtil.unserialize(jedis.get("bookConfigMapStatic".getBytes()));
			recordConfigMap = (Map<Integer, RecordConfigVo>) RedisUtil.unserialize(jedis.get("recordConfigStatic".getBytes()));
			planChapterConfigMap = (Map<Integer, PlanChapterConfigVo>) RedisUtil.unserialize(jedis.get("planChapterConfigStatic".getBytes()));
			levelConfigMap = (Map<Integer, LevelConfigVo>) RedisUtil.unserialize(jedis.get("levelConfigStatic".getBytes()));
			switchSettingMap = (Map<String,SwitchSettingVo>) RedisUtil.unserialize(jedis.get("switchSettingStatic".getBytes()));
		} catch (Exception e) {
			LoggerUtil.error(e);
		} finally {
			jedis.close();
		}
	}
	/**
	 * 初始化数据加载内存，同时存放redis
	 */
	public void valueInit(){
		Jedis jedis = RedisUtil.getJedis();
		try {
			/**
			 * 判断对应key是否在redis中存在数据，没有则添加
			 */
			//数据库查询、map封装、存入redis
			List<PlanConfigVo> ps = planConfig.getPlanConfigList();
			planConfigMap = ps.stream().collect(Collectors.toMap(PlanConfigVo::getPlanId, (pcv) -> pcv));
			jedis.set("planConfigStatic".getBytes(), RedisUtil.serialize(planConfigMap));
			
			List<BookConfigVo> bs = bookConfig.getBookConfigList();
			bookConfigMap = bs.stream().collect(Collectors.toMap(BookConfigVo::getBookId, (bcv) -> bcv));
			jedis.set("bookConfigMapStatic".getBytes(), RedisUtil.serialize(bookConfigMap));
			
			List<RecordConfigVo> rs = recordConfig.getRecordConfigList();
			recordConfigMap = rs.stream().collect(Collectors.toMap(RecordConfigVo::getWordId, (rcv) -> rcv));
			jedis.set("recordConfigStatic".getBytes(), RedisUtil.serialize(recordConfigMap));
			
			List<PlanChapterConfigVo> pcs = planChapterConfig.getPlanChapterList();
			planChapterConfigMap = pcs.stream().collect(Collectors.toMap(PlanChapterConfigVo::getPlanChapterId, (pccv) -> pccv));
			jedis.set("planChapterConfigStatic".getBytes(), RedisUtil.serialize(planChapterConfigMap));
			
			 List<LevelConfigVo> levelList =levelConfigService.getLevelList();
			 levelConfigMap = levelList.stream().collect(Collectors.toMap(LevelConfigVo::getId, (lcv) -> lcv));
			 jedis.set("levelConfigStatic".getBytes(), RedisUtil.serialize(levelConfigMap));
			
			 List<SwitchSettingVo> swiths =switchSettingService.getSwitchSettingList();
			 switchSettingMap = swiths.stream().collect(Collectors.toMap(SwitchSettingVo::getSwitchKey, (ssv) -> ssv));
			 jedis.set("switchSettingStatic".getBytes(), RedisUtil.serialize(switchSettingMap));
		} catch (Exception e) {
			LoggerUtil.error(e);
		}finally {
			jedis.close();
		}
	}
}
